package com.example.hasee.plant.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.plant.R;
import com.example.hasee.plant.config.MyApplication;

import java.io.File;

/**
 * Created by jyb on 2017/2/2.
 */

public class GatherImageFragment extends Fragment {
    private View view;
    private ImageView iv_img;
    private TextView tv_takePhoto;
    String imgPath;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.gather_img_frag, null);

        initView();
        getPath();
        tv_takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(MyApplication.IMG_PATH_FINISH!=null){
                    File file=new File(MyApplication.IMG_PATH_FINISH);
                    if(file.exists()){
                        file.delete();
                    }
                }
                startTakePhoto();
            }
        });
        return view;
    }

    private void initView() {
        iv_img = (ImageView) view.findViewById(R.id.iv_img);
        tv_takePhoto = (TextView) view.findViewById(R.id.tv_takePhoto);
    }

    private String getPath() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File file = new File(Environment.getExternalStorageDirectory().getPath() + "/plant" + MyApplication.IMG_PATH+"/"+MyApplication.GATHER_NUM+".jpg");
            Log.d("imgpath","path:"+file.getAbsolutePath());
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            imgPath=file.getAbsolutePath();
        } else {
            Toast.makeText(getActivity(), "sdk不在状态", Toast.LENGTH_SHORT).show();
        }
        return imgPath;
    }

    String mFilePath="";

    private void startTakePhoto() {

//        mFilePath= Environment.getExternalStorageDirectory().getPath();
        mFilePath=getPath();
//        mFilePath=mFilePath+"/"+ String.valueOf(System.currentTimeMillis()) +".jpg";
        Log.d("imgpath","path:"+mFilePath);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        Uri photoUri=Uri.fromFile(new File(mFilePath));
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(intent, 3);


    }

    private BitmapFactory.Options getBitmapOption(int inSampleSize){
        System.gc();
        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inPurgeable=true;
        options.inSampleSize=inSampleSize;
        return  options;
    }

    Bitmap photo;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == Activity.RESULT_OK) {
                if (requestCode == 3) {
                    String filepath=mFilePath;
                    MyApplication.IMG_PATH_FINISH=filepath;
                    Bitmap bitmap=BitmapFactory.decodeFile(filepath,getBitmapOption(2));
                    iv_img.setImageBitmap(bitmap);
//                    if (data.getData() != null || data.getExtras() != null) { //防止没有返回结果
//                        Uri uri = data.getData();
//                        if (uri != null) {
//                            photo = BitmapFactory.decodeFile(uri.getPath()); //拿到图片
//                        }
//                        if (photo == null) {
//                            Bundle bundle = data.getExtras();
//                            if (bundle != null) {
//                                photo = (Bitmap) bundle.get("data");
//                            } else {
//                                Toast.makeText(getActivity(), "找不到图片", Toast.LENGTH_SHORT).show();
//                            }
//                        }
//
//                        if(photo!=null){
//                            iv_img.setImageBitmap(photo);
//                        }
//
//                    }


                    super.onActivityResult(requestCode, resultCode, data);
                }
            }
    }
}

